﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YNCNumberSys
{
    public partial class Loading2 : UserControl, IDisposable
    {
        private System.Windows.Forms.Timer timer1;
        public Loading2()
        {
            InitializeComponent();
            this.Visible = true;//默认不显示
        }
        private void Close()
        {
            //自写Close方法释放资源
            this.Dispose();
        }
        private void Start(Control contorl)
        {
            this.Parent = contorl;
            ReLocation();//绘制位置到父级控件中间
            foreach (Control c in this.Parent.Controls)
            {
                if (this != c){ c.Enabled = false; }
            }
            this.Visible = false;//显示
            //this.SendToBack();
            this.timer1 = new System.Windows.Forms.Timer();
            this.timer1.Interval = 5000;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            timer1.Enabled = true;//开始计时
        }
        private void ReLocation()
        {
            int x = (int)(0.5 * (Parent.Width - this.Width));
            int y = (int)(0.5 * (Parent.Height - this.Height));
            this.Location = new System.Drawing.Point(x, y);
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
#if DEBUG
                MessageBox.Show("加载超时！！");
#endif
                timer1.Stop();
                Close();//超时关闭
            }
            catch (Exception ex)
            {
                //                LogHelper.Instance.Error("加载超时！" + ex.ToString());
            }
        }
        /// <summary>
        /// 使用new关键字覆盖原有System.Windows.Forms的Dispose
        /// </summary>
        private new void Dispose()
        {
            if (this.Disposing)
            {
                return;
            }
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() =>//在线程中调用，使用Invoke执行是否该用户控件代码
                {
                    if (this.Parent != null)
                    {
                        foreach (Control c in this.Parent.Controls)//启用父控件
                        {
                            c.Enabled = true;
                        }
                    }
                    Dispose(true); timer1.Dispose();
                }));
            }
            else
            {
                if (this.Parent != null)
                {
                    foreach (Control c in this.Parent.Controls)//启用父控件
                    {
                        c.Enabled = true;
                    }
                }
                Dispose(true); timer1.Dispose();
            }

        }

        static Task<Loading2> task = null;
        static CancellationTokenSource cancelTokenSource = null;
        public static void Show(System.Windows.Forms.Control parentContorl, Action load, Action after = null)
        {
            //当上一个任务还在执行时候，最新任务到来。则取消上个任务
            if (task != null)
            {
                cancelTokenSource.Cancel();
                task = null;
            }
            cancelTokenSource = new CancellationTokenSource();
            Loading2 w = new Loading2();
            w.Start(parentContorl);
            task = Task.Factory.StartNew(() =>
            {
                load.Invoke();//处理数据，里面对控件的操作需异步处理
                return w;
            }, cancelTokenSource.Token);
            task.ContinueWith(task =>
            {
                if (after != null)
                {
                    after.Invoke();
                }
                task.Result.Close();//无论异常/取消/完成 都执行关闭
            }, TaskContinuationOptions.AttachedToParent);
        }

    }
}
